import Https from "../../../utils/api"
const http = new Https
Page({
  data: {
    //房源租赁方式
    fangRent: [],
    //房源朝向
    directions: [],
    //房源配置
    fangConfig: [],
    //户型选择
    houseType: [],

    //数组的索引
    fangRentIndex: 0,
    directionsIndex: 0,
    houseTypeIndex: 0,
    fangConfigIndex: 0,
    //要入库数据的ID
    fangRentId: 0,
    directionsId: 0,
    fangConfigId: 0,
    houseTypeId: 0,

    //发布房源信息
    fang: {
      phone: '',
      price: '',
      housingAddress: '',
      username: '',
      housing_eatate: '',
      area: '',
    },
    //验证
    areaRules: {
      type: 'float',
      required: true,
      message: '面积类型不合法',
      trigger: 'change'
    },
    housing_eatateRules: {
      type: 'string',
      required: true,
      message: '小区名类型不合法',
      trigger: 'change'
    },
    addressRules: {
      type: 'string',
      required: true,
      message: '地址数据类型不合法',
      trigger: 'change'
    },
    priceRules: {
      type: 'float',
      required: true,
      message: '价格数据类型不合法',
      trigger: 'change'
    },
    phoneRules: {
      type: 'number',
      required: true,
      message: '手机号数据类型不合法',
      max: '',
      trigger: 'change'
    },
    usernameRules: {
      type: 'string',
      required: true,
      trigger: 'change',
      message: '用户名数据类型不合法',
    },
    //默认地区
    region: ['上海市', '上海市', '奉贤区'],
    customItem: '全部',
    //图片存储数据
    inDoor: [],
    outDoor: [],
    typeDoor: [],
    faceImg: '',
    //地区
    sheng:'',
    shi:'',
    qu:'',
  },
  onLoad: function () {
    wx.lin.initValidateForm(this);
    this.fangAttress();
  },
  //房源属性加载
  fangAttress() {
    http.request('fang/attress', {
    }).then(res => {
      //保存数据
      if (res.code == 200) {
        let directions = res.data.directions;
        let houseType = res.data.house_type;
        let fangRent = res.data.fang_rent;
        let fangConfig = res.data.fang_config;
        this.setData({
          //朝向
          directions,
          //户型
          houseType,
          //租赁方式
          fangRent,
          //房屋配置
          fangConfig,
        })
      }
    })
  },
  submit(event) {
    const { detail } = event;
    //选择属性id
    let fangRentId = this.data.fangRentId;
    let directionsId = this.data.directionsId;
    let fangConfigId = this.data.fangConfigId;
    let houseTypeId = this.data.houseTypeId;
    //文件图片url
    let inDoor = this.data.inDoor;
    let outDoor = this.data.outDoor;
    let typeDoor = this.data.typeDoor;
    let faceImg = this.data.faceImg;
    //其他信息
    let housingAddress = event.detail.values.housingAddress;
    let phone = event.detail.values.phone;
    let price = event.detail.values.price;
    let username = event.detail.values.username;
    let housing_eatate = event.detail.values.housing_eatate;
    let area = event.detail.values.area;
    //城市信息
    let sheng = this.data.sheng;
    let shi = this.data.shi;
    let qu = this.data.qu;
    http.request("fang/outRent/create", {
      fangRentId,
      directionsId,
      fangConfigId,
      houseTypeId,
      inDoor,
      outDoor,
      typeDoor,
      faceImg,
      housingAddress,
      phone,
      price,
      username,
      housing_eatate,
      area,
      sheng,
      shi,
      qu,
      type:2,//出租方式
    }, "POST").then((res) => {
      if (res.code == 200) {
        wx.showToast({
          title: '添加成功',
          icon: "none"
        })
      } else {
        wx.showToast({
          title: '添加失败',
          icon: "none"
        })
      }
    })
  },
  //朝向选择器
  directionsChange(e) {
    var indes = e.detail.value;
    //根据索引取出数据的id
    var dataid = this.data.directions[indes]['id'];
    this.setData({
      directionsIndex: indes,
      directionsId: dataid
    })
  },
  //户型选择器
  houseTypeChange: function (e) {
    var indes = e.detail.value;
    //根据索引取出数据的id
    var dataid = this.data.houseType[indes]['id'];
    this.setData({
      houseTypeIndex: indes,
      houseTypeId: dataid
    })
  },
  //房源配置
  fangConfigChange: function (e) {
    var indes = e.detail.value;
    //根据索引取出数据的id
    var dataid = this.data.fangConfig[indes]['id'];
    this.setData({
      fangConfigIndex: indes,
      fangConfigId: dataid
    })
  },
  fangRentChange: function (e) {
    var indes = e.detail.value;
    //根据索引取出数据的id
    var dataid = this.data.fangRent[indes]['id'];
    this.setData({
      fangRentIndex: indes,
      fangRentId: dataid
    })
  },
  //户型选择
  houseTypeChange: function (e) {
    var indes = e.detail.value;
    //根据索引取出数据的id
    var dataid = this.data.houseType[indes]['id'];
    this.setData({
      houseTypeIndex: indes,
      houseTypeId: dataid
    })
  },
  //城市选择器
  bindRegionChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.postcode)
    this.setData({
      region: e.detail.value,
      sheng:e.detail.value[0],
      shi:e.detail.value[1],
      qu:e.detail.value[2],
    })
  },
  //房屋图片分类
  typeDoor(e) {
    this.setData({
      typeDoor: e.detail.all
    })
    let img = e.detail.current[0];
    this.upfile(img, 'typeDoor');
  },
  //室内图
  inDoor(e) {
    this.setData({
      inDoor: e.detail.all
    })
    let img = e.detail.current[0];
    this.upfile(img, 'inDoor');
  },
  //室外图
  outDoor(e) {
    let img = e.detail.current[0];
    this.upfile(img, 'outDoor')
  },
  faceImg(e) {
    let img = e.detail.current[0];
    this.upfile(img, 'faceImg')
  },
  //文件上传
  upfile(img, type) {
    wx.uploadFile({
      url: 'http://local.da.com/api/upfile', //仅为示例，非真实的接口地址
      filePath: img,
      name: 'file',
      header: {
        'Content-Type': 'application/json;charset=UTF-8',
        'token': wx.getStorageSync('token').token
      },
      success: (res) => {
        let imgPath = JSON.parse(res.data)
        if (type == 'outDoor') {
          this.setData({
            outDoor: [...this.data.outDoor, imgPath.data]
          })
        } else if (type == 'inDoor') {
          this.setData({
            inDoor: [...this.data.inDoor, imgPath.data]
          })
        } else if (type == 'typeDoor') {
          this.setData({
            typeDoor: [...this.data.typeDoor, imgPath.data]
          })
        } else if (type == 'faceImg') {
          this.setData({
            faceImg: imgPath.data
          })
        }
      },
      fail(res) {
        console.log(res)
      }
    })
  },
})